package com.jenkins;

import java.io.*;

/**
 * Jenkins Test Demo
 *
 * @author Je
 */
public class Main {
    public static void main(String[] args) {
        //new ReadFileThreadV2().start();
        new ReadFileThread().start();

        //String adbHome = "D:\\Tools\\AndroidSDK\\platform-tools";
        String cmd = "adb shell getevent -tl ";
        //String cmd = "adb shell getevent -t ";
        Process process;
        try {
            process = Runtime.getRuntime().exec(cmd);
            //System.out.println(InputStream2String(process.getInputStream()));
            writeToLocal("F:/test/test.txt", process.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String InputStream2String(InputStream inputStream) {
        StringBuilder result = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        try {
            String temp = "";
            while ((temp = br.readLine()) != null) {
                result.append(temp).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }


    /**
     * 将InputStream写入本地文件
     *
     * @param destination 写入本地目录
     * @param input       输入流
     * @throws IOException IOException
     */
    public static void writeToLocal(String destination, InputStream input)
            throws IOException {
        int index;
        byte[] bytes = new byte[10240];
        FileOutputStream downloadFile = new FileOutputStream(destination);
        while ((index = input.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        input.close();
        downloadFile.close();

    }
}
